#include "core/plugin/plugin_manager.h"
#include "gui/main_window.h"
#include <QDebug>

PluginManager::PluginManager(MainWindow *mainWindow, QObject *parent)
    : QObject(parent), m_mainWindow(mainWindow) {
}

PluginManager::~PluginManager() {
    unloadPlugins();
}

void PluginManager::loadPlugins() {
    QDir pluginsDir(qApp->applicationDirPath());
    pluginsDir.cd("plugins");
    
    for (const QString &fileName : pluginsDir.entryList(QDir::Files)) {
        loadPlugin(pluginsDir.absoluteFilePath(fileName));
    }
}

void PluginManager::unloadPlugins() {
    for (QPluginLoader *loader : m_pluginLoaders) {
        PluginInterface *plugin = qobject_cast<PluginInterface*>(loader->instance());
        if (plugin) {
            plugin->shutdown();
        }
        loader->unload();
        delete loader;
    }
    
    m_pluginLoaders.clear();
    m_plugins.clear();
}

QList<PluginInterface*> PluginManager::plugins() const {
    return m_plugins;
}

void PluginManager::loadPlugin(const QString &filePath) {
    QPluginLoader *loader = new QPluginLoader(filePath, this);
    QObject *plugin = loader->instance();
    
    if (!plugin) {
        qWarning() << "无法加载插件" << filePath << ":" << loader->errorString();
        delete loader;
        return;
    }
    
    PluginInterface *pluginInterface = qobject_cast<PluginInterface*>(plugin);
    if (!pluginInterface) {
        qWarning() << "无效的插件接口" << filePath;
        loader->unload();
        delete loader;
        return;
    }
    
    pluginInterface->initialize(m_mainWindow);
    m_pluginLoaders.append(loader);
    m_plugins.append(pluginInterface);
    
    qDebug() << "加载插件:" << pluginInterface->name() 
             << pluginInterface->version();
}
修改MainWindow支持插件
src/gui/main_window.cpp (修改)
cpp
// 在MainWindow构造函数中添加
pluginManager = new PluginManager(this, this);
pluginManager->loadPlugins();